package com.dhh.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;

import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.CUtil;
import com.dhh.util.manager.WeightManager;
import com.dhh.vo.Weight;
import com.flotandroidchart.flot.FlotChartContainer;
import com.flotandroidchart.flot.FlotDraw;
import com.flotandroidchart.flot.Options;
import com.flotandroidchart.flot.data.PointData;
import com.flotandroidchart.flot.data.SeriesData;
import com.flotandroidchart.flot.data.TickData;
import greendroid.app.GDActivity;

public class WeightChartActivity extends GDActivity{	
	private DataBaseHelper db;
	private WeightManager wManager;
	
	@Override  
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setActionBarContentView(R.layout.weight_chart);
		initActionBar();
		
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();         
        wManager = new WeightManager(db);     
	}
	
	@Override
	public void onStart() {
        super.onStart();      

		initChart();
	}
	
	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}
	
	private void initActionBar(){
		getActionBar().setTitle("My Weight");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(WeightChartActivity.this,HomeActivity.class);	
					startActivity(intent);
				}}); 
	}
	
	public void initChart(){
    	List<Weight> lst = wManager.getChartData(0);    	
    	List<Weight> wl = new ArrayList<Weight>();
    	for(int i=lst.size()-1; i>=0; i--){
    		wl.add(lst.get(i));
    	}   	
    	
    	Vector<SeriesData> sds = new Vector<SeriesData>();	
    	
		SeriesData sd1 = new SeriesData();		
		Vector<PointData> pds1 = new Vector<PointData>();		
		for(int i=0; i < wl.size(); i++) {
			pds1.add(new PointData(i, Double.parseDouble(wl.get(i).getWeight())));
		}
		sd1.setData(pds1);
		
		if(pds1.size()>0)
			sds.add(sd1);
		
        Options opt = new Options();
        opt.grid.hoverable = false;
        
		opt.series.lines.setShow(true);
		opt.series.points.show = true;	
		opt.yaxis.tickDecimals = 1;
		Vector<TickData> ticks = new Vector<TickData>();
		for(int i=0; i<wl.size(); i+=4){
			ticks.add(new TickData(i,CUtil.getDate("yyyy-MM-dd", "MM/dd", wl.get(i).getDate())));
		}
		opt.xaxis.ticks = ticks;
		
		FlotDraw fd = new FlotDraw(sds, opt, null);
        FlotChartContainer tv = (FlotChartContainer)this.findViewById(R.id.wc_chart);
        if(tv != null) {
            tv.setDrawData(fd);
        }
    }

}
